﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tester.DL645.Responses
{
    public class ResponseBuilder
    {
        /// <summary>
        /// 
        /// </summary>
        public ResponseBuilder()
        {
            
        }

        public bool IsCommandIdentifier(byte[] buffer, byte[] commandIdentifier)
        {
            for (int i = 0; i < 4; i++)
            {
              var flag=   commandIdentifier[i] == buffer[10 + i];
                if (!flag)
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public bool IsEnergyCommandIdentifier(byte[] buffer)
        {

            int d0Index = 10;
            int d1Index = 11;
            int d2Index = 12;
            int d3Index = 13;
            return buffer[d0Index] == Core.Constants.EnergyCommandIdentifier[0]
              && buffer[d1Index] == Core.Constants.EnergyCommandIdentifier[0]
              && buffer[d2Index] == Core.Constants.EnergyCommandIdentifier[0]
              && buffer[d3Index] == Core.Constants.EnergyCommandIdentifier[0];

        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="responseType"></param>
        /// <returns></returns>
        public AbstractReponse CreateResponse(byte[] buffer, Core.ResponseType responseType)
        {
            int cmdIndex = 8;

            
            var cmd = buffer[cmdIndex].ToString("X2");
       

            if(buffer.Length<9)
            {
                return new ErrorReponse();
            }
            ///
            if (responseType== Core.ResponseType.Address 
                && buffer[cmdIndex] == 0x93)
            {
                return new AddressReponse(buffer);
            } 
            else if (responseType == Core.ResponseType.On && buffer[cmdIndex] == 0x9C)
            {
                return new OnReponse(buffer);
            }
            else if (responseType == Core.ResponseType.Off && buffer[cmdIndex] == 0x9C)
            {
                return new OffReponse(buffer);
            }
            else if (responseType == Core.ResponseType.Energy 
                && buffer[cmdIndex] == 0X91
                && IsCommandIdentifier(buffer,Core.Constants.EnergyCommandIdentifier)
                )
            {
                return new EnergyResponse(buffer);
            }
            else if (responseType == Core.ResponseType.APhaseVoltage
                && buffer[cmdIndex] == 0X91
                && IsCommandIdentifier(buffer, Core.Constants.APhaseVoltageCommandIdentifier)
                )
            {
                return new PhaseVoltageResponse(buffer, Core.Phase.A);
            }
            else if (responseType == Core.ResponseType.APhaseCurrent 
                && buffer[cmdIndex] == 0X91
                && IsCommandIdentifier(buffer, Core.Constants.APhaseCurrentCommandIdentifier)
)
            {
                return new PhaseCurrentResponse(buffer, Core.Phase.A);
            }

            return new ErrorReponse();
        }
    }
}
